package com.scpii.universal.struts.manage;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.doone.comm.paginaction.Pagination;
import com.scpii.universal.action.BaseAction;
import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.dto.ActiveUserDto;
import com.scpii.universal.service.ActiveUserService;

@Controller
@Scope("prototype")
public class ActiveManageAction extends BaseAction {
	@Resource
	private ActiveUserService activeUserService;
	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -2810450416573568925L;
	// 分页信息
	private Pagination page = new Pagination();

	private Pagination pageActive = new Pagination();
	// 查询条件
	private ActiveUserDto condition;

	private List<ActiveUserDto> allActiveList;

	private List<ActiveUserDto> actList;

	private String intContentId;

	public String getActiveList() {

		if (condition == null) {
			condition = new ActiveUserDto();
			// condition.setIntAppId(getUserInfo().getAppId());
		}
		// 判断第一次进入分页信息
		if (getSelectPerPage("page") == -1) {
			page.setPageRecord(GenericConstants.PAGE_NO);
		} else {
			page.setPageRecord(getSelectPerPage("page"));
		}

		// 判断是否是第一次查询

		List<ActiveUserDto> countList = activeUserService.getAllActiveCount(getUserInfo().getAppId());
		if (getQueryFlag("page") == -1) {
			page.setTotalRecord(countList.size());
		} else {
			page.setTotalRecord(getQueryFlag("page"));
		}
		setPageParameters(page, "page");

		List<ActiveUserDto> list = activeUserService.getAllActiveList(condition, page.getCurrentPage(),
				page.getPageRecord(), getUserInfo().getAppId());

		if (list != null && list.size() > 0) {
			List<ActiveUserDto> tempList = new ArrayList<ActiveUserDto>();
			int temp = 0;
			for (ActiveUserDto tempDto : list) {
				if (temp == tempDto.getIntContentId()) {
					tempList.add(tempDto);
				}
				temp = tempDto.getIntContentId();
			}
			list.removeAll(tempList);
			int size = list.size();
			int start = (page.getCurrentPage() - 1) * page.getPageRecord();
			int end = size - start > 5 ? 5 : size - start;

			allActiveList = new ArrayList<ActiveUserDto>();

			for (int i = start; i < start + end; i++) {
				ActiveUserDto tempDto = list.get(i);
				tempDto.setActiveNum(activeUserService.getActivelistCount(tempDto.getIntContentId()));
				allActiveList.add(tempDto);
			}
		}

		return SUCCESS;

	}

	public String getActivePerson() {
		//获取内容Id
		String id=request.getParameter("intContentId");
		if (condition == null) {
			condition = new ActiveUserDto();
			condition.setIntContentId(Integer.parseInt(id));
			// condition.setIntAppId(getUserInfo().getAppId());
		}
		
		// 判断第一次进入分页信息
		if (getSelectPerPage("pageActive") == -1) {
			pageActive.setPageRecord(GenericConstants.PAGE_NO);
		} else {
			pageActive.setPageRecord(getSelectPerPage("pageActive"));
		}

		// 判断是否是第一次查询
		if (getQueryFlag("pageActive") == -1) {
			pageActive.setTotalRecord(activeUserService.getActivelistCount(Integer.parseInt(id)));
		} else {
			pageActive.setTotalRecord(getQueryFlag("pageActive"));
		}
		setPageParameters(pageActive, "pageActive");

		actList = activeUserService.getActiveLsit(condition, pageActive.getCurrentPage(),
				pageActive.getPageRecord());
		intContentId=null;
		return SUCCESS;
	}

	public Pagination getPage() {
		return page;
	}

	public void setPage(Pagination page) {
		this.page = page;
	}

	public Pagination getPageActive() {
		return pageActive;
	}

	public void setPageActive(Pagination pageActive) {
		this.pageActive = pageActive;
	}

	public ActiveUserDto getCondition() {
		return condition;
	}

	public void setCondition(ActiveUserDto condition) {
		this.condition = condition;
	}

	public List<ActiveUserDto> getAllActiveList() {
		return allActiveList;
	}

	public void setAllActiveList(List<ActiveUserDto> allActiveList) {
		this.allActiveList = allActiveList;
	}

	public List<ActiveUserDto> getActList() {
		return actList;
	}

	public void setActList(List<ActiveUserDto> actList) {
		this.actList = actList;
	}

	public Integer getIntContentId() {
		return Integer.parseInt(intContentId);
	}

	public void setIntContentId(String intContentId) {
		this.intContentId = intContentId;
	}

	
}
